package cn.zjitc.controller;

import cn.zjitc.constant.MessageConstant;
import cn.zjitc.entity.PageResult;
import cn.zjitc.entity.QueryPageBean;
import cn.zjitc.entity.Result;
import cn.zjitc.pojo.Book;
import cn.zjitc.pojo.Borrow;
import cn.zjitc.pojo.History;
import cn.zjitc.pojo.User;
import cn.zjitc.service.UserBookService;
import com.alibaba.dubbo.config.annotation.Reference;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.List;

@RestController
@RequestMapping("/userbook")
public class UserBookController {
    @Reference
    private UserBookService userBookService;

    //借阅书籍
    @GetMapping("/add")
    public Result add(Integer id,String name,HttpSession session){
        Date date=new Date();
        Calendar calendar=new GregorianCalendar();
        try{
            User user = (User) session.getAttribute("user");
            Borrow borrow=new Borrow();
            borrow.setBook_id(id);
            String neoname = new String(name.getBytes("ISO-8859-1"), "UTF-8");
            borrow.setBook_name(neoname);
            borrow.setUser_id(user.getUser_id());
            borrow.setBorrow_time(date);
            calendar.setTime(borrow.getBorrow_time());
            Integer endtime=userBookService.getendtime(borrow.getUser_id());
            calendar.add(Calendar.DATE,endtime);
            borrow.setEnd_time(calendar.getTime());
            Integer remainnum=userBookService.findremainnum(id);
            if(remainnum-1>0){
                borrow.setBook_state(1);
                borrow.setBorrow_state(0);
                userBookService.add(borrow);
                userBookService.editbooklist(id);
                userBookService.edituserborrow(user.getUser_id());
            }
            if(remainnum-1==0){
                borrow.setBook_state(0);
                borrow.setBorrow_state(0);
                userBookService.add(borrow);
                userBookService.editbooklist(id);
                userBookService.edituserborrow(user.getUser_id());
            }
            History history=new History();
            history.setBook_id(id);
            history.setBook_name(neoname);
            history.setBorrow_time(date);
            history.setUser_id(user.getUser_id());
            history.setUser_name(user.getUser_name());
            history.setBorrow_state(0);
            userBookService.addHistory(history);
        }catch (Exception e){
            e.printStackTrace();
            return new Result(false,"借阅书籍失败");
        }
        return new Result(true,"借阅书籍成功");
    }

    //查询所有书籍情况
    @RequestMapping("/findAll")
    public Result findAll(){
        try {
            List<Book> list= userBookService.findAll();
            return new Result(true, MessageConstant.ADMIN_SEARCH_SUCCESS,list);
        }catch (Exception e){
            e.printStackTrace();
            return new Result(false,MessageConstant.ADMIN_SEARCH_FAIL);
        }
    }

    //根据书籍id查询
    @RequestMapping("/findById")
    public Result findById(Integer id){
        try {
            Book book = userBookService.findById(id);
            return new Result(true,MessageConstant.ADMIN_SEARCH_SUCCESS, book);
        }catch (Exception e){
            e.printStackTrace();
            return new Result(false,MessageConstant.ADMIN_SEARCH_FAIL);
        }
    }

    //分页查询
    @RequestMapping("/findPage")
    public PageResult findPage(@RequestBody QueryPageBean queryPageBean){
        PageResult pageResult= userBookService.pageQuery(queryPageBean);
        return pageResult;
    }
}



